package cmdParser;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CmdParser {
	public static String[] cmdParseForClient(String cmd){
		String[] cmdParams = new String[4];
		
		String r = "((SCP .+ \\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}:/.+)|LS|RM .+)";
		Pattern p = Pattern.compile(r);
		Matcher m = p.matcher(cmd);
		if(!m.find()){
			System.out.println("not find");
			return null;
		}
		else{
			String[] str  = cmd.split(" ");
			
			if(str.length == 1){
				cmdParams[0] = str[0];
			}
			else if(str.length == 2){
				cmdParams[0] = str[0];
				cmdParams[1] = str[1];
			}
			else if(str.length == 3){
				cmdParams[0] = str[0];
				cmdParams[1] = str[1];
				
				String[] buf = str[2].split(":");
				cmdParams[2] = buf[0];
				cmdParams[3] = buf[1];
			}
			return cmdParams;
		}
	}
	
	public static String[] cmdParseForServer(String cmd){		//返回文件名
		String r = "(((SCP|RM) .+)|LS)";
		Pattern p = Pattern.compile(r);
		Matcher m = p.matcher(cmd);
		if(!m.find()){
			System.out.println("not find");
			return null;
		}
		else{
			String[] buf = cmd.split(" ");
//			for(String i:buf) System.out.println(i);
			return buf;
		}
	}
}
